package MaxHeap;

import ThreadTest.TestDemo;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import com.sun.org.apache.bcel.internal.generic.NEW;

import java.util.Random;

/**
 * TOOD
 *
 * @author long
 * @date 2020-01-08 16:09
 */
public class Main {

    private static double testHeap(Integer[] testData,boolean isHeapIfy){

        long startTime=System.nanoTime();

        MaxHeap<Integer> maxHeap;
        if (isHeapIfy){
            maxHeap=new MaxHeap<>(testData);
        }else {
            maxHeap=new MaxHeap<Integer>();
            for (int num:testData){
                maxHeap.add(num);
            }
        }
        int[] arr=new int[testData.length];
        for (int i=0;i<testData.length;i++){
            arr[i]=maxHeap.extractMax();
        }
        for (int i=1;i<testData.length;i++){
            if (arr[i-1]<arr[i]){
                throw new IllegalArgumentException("Error");
            }

        }
        System.out.println("Test MaxHeap Completed.");
        long endTime=System.nanoTime();

        return (endTime-startTime)/1000000000.0;
    }


    public static void main(String[] args) {
        int n=10000000;
        Random random = new Random();
        Integer[] testData = new Integer[n];
        for (int i=0;i<n;i++){
            testData[i]=random.nextInt(Integer.MAX_VALUE);
        }
        double t1 = testHeap(testData, false);
        System.out.println("without Heapify:"+t1+"s");
        double t2 = testHeap(testData, true);
        System.out.println("with Heapify:"+t2+"s");
    }

}
